<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <h3>用户登陆</h3>
    {{errorMessage}}
    <hr/>
    账号：<input type="text" v-model="user.userCode"/><br/>
    密码：<input type="password" v-model="user.password"/><br/>
    <button @click="login">登陆</button>
</div>
</body>
</html>
<script src="js/vue.min.js"></script>
<script src="js/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                user: {
                    userCode: '',
                    password: ''
                },
                errorMessage: ''
            }
        },
        methods: {
            login() {
                let _this = this;
                let data = new URLSearchParams();
                data.append("userCode", this.user.userCode)
                data.append("password", this.user.password)
                axios({
                    method: 'post',
                    url: 'user/login',
                    data: data
                }).then((response) => {
                    if (response.data.success) {
                        // 保存 token 字符串
                        window.localStorage.setItem('token', response.data.data)
                        // 跳转到首页
                        location.href = 'index.html'
                    } else {
                        _this.errorMessage = response.data.errorMsg
                    }
                })
            }
        }
    })
</script>